import Vue from "vue";
import Vuex from "vuex";
import form from "./modules/form";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    token: "",
    userInfo:
      JSON.stringify(sessionStorage.getItem("userInfo")) == "{}"
        ? JSON.parse(sessionStorage.getItem("userInfo"))
        : {},
    role: ""
  },
  mutations: {
    // set
    SET_TOKEN: (state, token) => {
      state.token = token;
      localStorage.setItem("token", token);
    },
    SET_USERINFO: (state, userInfo) => {
      state.userInfo = userInfo;
      sessionStorage.setItem("userInfo", JSON.stringify(userInfo));
    },
    // set role
    // teacher md5(t././.as,16) = 4eb118447d8e3cf0
    // student md5(s././.as,16) = de72303d2cb6ea86
    SET_ROLE: (state, role) => {
      state.role = role;
      sessionStorage.setItem("cm9sZQ==", role);
    },
    REMOVE_INFO: state => {
      (state.token = ""),
        (state.userInfo = {}),
        localStorage.setItem("token", ""),
        sessionStorage.setItem("userInfo", JSON.stringify("{}")),
        sessionStorage.setItem("cm9sZQ==", "");
    }
  },
  getters: {
    // get
    getUser: state => {
      state.userInfo = JSON.parse(sessionStorage.getItem("userInfo"));
      return state.userInfo;
    },
    getRole: state => {
      state.role = sessionStorage.getItem("userInfo");
      return state;
    }
  },
  actions: {},
  modules: {
    form
  }
});
